﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Threading;
using System.Diagnostics;
using System.Collections;
//using System.ServiceModel

namespace rss
{
    public partial class Form1 : Form
    {

        public string url;


        public Form1()
        {
            InitializeComponent();

        }


        SyndicationFeed feed;
        bool flag;
        /// <summary>
        /// 访问
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            feed = null;
            Uri uri = null;
            //xin jian
            if (feedAddress.Text != "")
            {
                uri = new Uri(feedAddress.Text);
                WebRequest request = (WebRequest)WebRequest.Create(uri);
                flag = false;
                request.BeginGetResponse(new AsyncCallback(responseReady), request);
                TreeNode treeNode = PostsList.Nodes[0];
                while (feed == null && flag == false)
                {
                    Thread.Sleep(1000);
                }
                if (feed != null)
                {
                    treeNode.Nodes.Clear();
                    foreach (SyndicationItem item in feed.Items)
                    {
                        TreeNode treeNode1 = new TreeNode();
                        treeNode1.Text = item.Title.Text + " " + item.PublishDate.ToLocalTime();
                        treeNode1.Tag = item;
                        treeNode.Nodes.Add(treeNode1);

                    }
                    treeNode.Expand();
                }
            }
            //xml
            else
            {
                if (PostsList.SelectedNode.Tag is string)
                {
                    uri = new Uri(PostsList.SelectedNode.Tag.ToString());
                    WebRequest request = (WebRequest)WebRequest.Create(uri);
                    request.BeginGetResponse(new AsyncCallback(responseReady), request);
                    flag = false;
                    TreeNode treeNode = PostsList.SelectedNode;
                    while (feed == null && flag == false)
                    {
                        Thread.Sleep(1000);
                    }
                    if (feed != null)
                    {
                        treeNode.Nodes.Clear();
                        foreach (SyndicationItem item in feed.Items)
                        {
                            TreeNode treeNode1 = new TreeNode();
                            treeNode1.Text = item.Title.Text + " " + item.PublishDate.ToLocalTime();
                            treeNode1.Tag = item;
                            treeNode.Nodes.Add(treeNode1);

                        }
                        treeNode.Expand();
                    }
                }
                else
                {
                    return;
                }
            }



        }
        void responseReady(IAsyncResult asyncResult)
        {
            try
            {

                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = true;
                WebRequest request = (WebRequest)asyncResult.AsyncState;
                WebResponse response = (WebResponse)request.EndGetResponse(asyncResult);
                XmlReader reader = XmlReader.Create(response.GetResponseStream());
                feed = SyndicationFeed.Load(reader);
                //PostsList.DataSource = feed.Items;
                //PostsList.DisplayMember = "Title.Text";

                flag = true;


            }
            catch (Exception ee)
            {
                flag = true;
                MessageBox.Show(ee.ToString());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PostsList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (PostsList.SelectedNode.Tag == null) { }
            else if (PostsList.SelectedNode.Tag is SyndicationItem)
            {
                SyndicationItem item = PostsList.SelectedNode.Tag as SyndicationItem;
                Detail.DocumentText = item.Summary.Text;
            }
            else if (PostsList.SelectedNode.Tag is string)
            {
                expend_rss(PostsList.SelectedNode, PostsList.SelectedNode.Tag.ToString());
            }

        }


        private void expend_rss(TreeNode treeNode, string url)
        {
            Uri uri = new Uri(url);
            WebRequest request = (WebRequest)WebRequest.Create(uri);
            request.BeginGetResponse(new AsyncCallback(responseReady), request);
            flag = false;



            while (feed == null && flag == false)
            {
                Thread.Sleep(1000);
            }
            if (feed != null)
            {
                treeNode.Nodes.Clear();
                foreach (SyndicationItem item in feed.Items)
                {
                    TreeNode treeNode1 = new TreeNode();
                    treeNode1.Text = item.Title.Text + " " + item.PublishDate.ToLocalTime();
                    //treeNode1.Tag = item;
                    //ArrayList arrayListTag = new ArrayList();
                    //arrayListTag.Add(item);
                    //arrayListTag.Add("rss");
                    treeNode1.Tag = item;

                    treeNode.Nodes.Add(treeNode1);

                }
                treeNode.Expand();
            }

        }


        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            TreeNode treeNode = new TreeNode();
            treeNode.Text = "root";

            PostsList.Nodes.Add(treeNode);


            if (string.IsNullOrEmpty(url))
            {
                XmlDocument doc = new XmlDocument();
                string strPath = "1.xml";
                doc.Load(strPath);
                XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
                foreach (XmlNode xn in nodeList)
                {
                    TreeNode treeNode1 = new TreeNode();
                    treeNode1.Text = xn.Attributes["name"].Value;
                    treeNode1.Tag = xn.Attributes["url"].Value;
                    treeNode.Nodes.Add(treeNode1);
                }
            }
            else
            {
                TreeNode treeNode1 = new TreeNode();
                treeNode1.Text = url;
                treeNode1.Tag = url;
                treeNode.Nodes.Add(treeNode1);

                //expend_rss(treeNode1, treeNode1.Tag.ToString());
            }
            treeNode.ExpandAll();

        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {

            XmlDocument doc = new XmlDocument();
            string strPath = "1.xml";
            doc.Load(strPath);
            TreeNode treeNode = PostsList.Nodes[0];
            treeNode.Nodes.Clear();
            XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                TreeNode treeNode1 = new TreeNode();
                if (xn.Attributes["name"].Value != "")
                {
                    treeNode1.Text = xn.Attributes["name"].Value;
                }
                else
                {
                    treeNode1.Text = xn.Attributes["url"].Value;
                }
                treeNode1.Tag = xn.Attributes["url"].Value;
                treeNode.Nodes.Add(treeNode1);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Process.Start("notepad", "1.xml");
        }

        /// <summary>
        /// dakai
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (this.PostsList.SelectedNode.Tag is SyndicationItem)
            {
                Process.Start((this.PostsList.SelectedNode.Tag as SyndicationItem).Links[0].Uri.ToString());
            }
        }
    }
}
